package com.demo.spring.autowird;

import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;

public class Autowired底层演示2 {
	
	public static void main(String[] args) {
		// GenericApplicationContext是一个干净的容器
		GenericApplicationContext context = new GenericApplicationContext();
		context.registerBeanDefinition("bean2", new AnnotatedGenericBeanDefinition(Bean2.class));
		context.registerBeanDefinition("bean3", new AnnotatedGenericBeanDefinition(Bean3.class));
		
		// 用于解析String等类型的属性
		context.getDefaultListableBeanFactory().setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
		// 查找哪些属性、方法加了@Autowired，这称之为InjectionMetadata
		AutowiredAnnotationBeanPostProcessor processor = new AutowiredAnnotationBeanPostProcessor();
		processor.setBeanFactory(context.getDefaultListableBeanFactory());
		
		// 手动创建bean1，此时肯定没有依赖注入
		Bean1 bean1 = new Bean1();
		System.out.println(bean1);
		// 执行依赖注入@Autowired、@Value
		processor.postProcessPropertyValues(null, null, bean1, "bean1");
		System.out.println(bean1);
	}

}
